//
//  SNKNewsNavController.m
//  SleepNews
//
//  Created by Kavee DJ on 2016/12/8.
//  Copyright © 2016年 Kavee DJ. All rights reserved.
//

#import "SNKNewsNavController.h"
#import "SNKHeadNewsController.h"
#import "SNKOtherNewsController.h"

#import "SNKNavTabBar.h"

#import <DKNightVersion.h>

@interface SNKNewsNavController () <UIScrollViewDelegate,SNKNavTabBarDelegate>
{
    NSInteger            _currentIndex;
    NSMutableArray       *_titles;
    SNKNavTabBar         *_navTabBar;
    UIScrollView         *_mainView;
}

@end

@implementation SNKNewsNavController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.dk_backgroundColorPicker = DKColorPickerWithKey(BG);
    
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    [self setupControllers];
    
    [self setupConfigure];
    
    [self setupUI];
}

- (void)setupControllers {
    
    NSArray *nameArray = [NSArray array];
    // 第一个单独设置 "头条"
    nameArray = @[@"新闻",@"财经",@"体育",@"娱乐",@"军事",@"教育",@"科技",@"NBA",@"股票",@"星座",@"女性",@"健康",@"育儿"];
    
    
    NSArray *contentArray = [NSArray array];
    contentArray = @[@"新闻",@"财经",@"体育",@"娱乐",@"军事",@"教育",@"科技",@"NBA",@"股票",@"星座",@"女性",@"健康",@"育儿"];
    
    NSMutableArray *viewArray = [NSMutableArray array];
    
    SNKHeadNewsController *oneViewController = [[SNKHeadNewsController alloc] init];
    oneViewController.title = @"头条";
    [viewArray addObject:oneViewController];
    
    for (NSInteger i = 0; i < nameArray.count; i++) {
        SNKOtherNewsController *otherViewController = [[SNKOtherNewsController alloc] init];
        otherViewController.title = nameArray[i];
        otherViewController.content = contentArray[i];
        [viewArray addObject:otherViewController];
    }
    
    _subViewControllers = [NSArray array];
    _subViewControllers = viewArray;
}

- (void)setupConfigure {
    _currentIndex = 1;
    
    _titles = [[NSMutableArray alloc] initWithCapacity:_subViewControllers.count];
    
    for (UIViewController *viewController in _subViewControllers) {
        
        [_titles addObject:viewController.title];
    }
}

- (void)setupUI {
    [self setupView];
    
    // 首先加载第一个视图
    UIViewController *viewController = (UIViewController *)_subViewControllers[0];
    viewController.view.frame = CGRectMake(0, 0, SCREENW, SCREENH);
    [_mainView addSubview:viewController.view];
    [self addChildViewController:viewController];
}

- (void)setupView {
    // 加载导航条
    _navTabBar = [[SNKNavTabBar alloc] initWithFrame:CGRectMake(0, 0, SCREENW, 64)];
    _navTabBar.backgroundColor = [UIColor clearColor];
    _navTabBar.delegate = self;
    _navTabBar.lineColor = _navTabBarLineColor;
    _navTabBar.itemTitles = _titles;
    [_navTabBar updateData];
    [self.view addSubview:_navTabBar];
    
    // 加载ScrollView
    _mainView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, SCREENW, SCREENH - 64)];
    _mainView.delegate = self;
    _mainView.pagingEnabled = YES;
    _mainView.bounces = _canMainViewBounces;
    _mainView.showsHorizontalScrollIndicator = NO;
    _mainView.showsVerticalScrollIndicator = NO;
    _mainView.contentSize = CGSizeMake(SCREENW * _subViewControllers.count, 0);
    [self.view addSubview:_mainView];
    
    // 导航条下分割线,就是这条白线
//    UIView *lineV = [[UIView alloc] initWithFrame:CGRectMake(0, 64, SCREENW, 1)];
//    lineV.backgroundColor = [UIColor colorWithRed:216/255.0f green:216/255.0f blue:216/255.0f alpha:1];
//    [self.view addSubview:lineV];
    
    // 右边按钮
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(SCREENW - 30, 32, 20, 20)];
    [btn setImage:[UIImage imageNamed:@"search"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(searchClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
}

-(void)searchClick
{
    KLog(@"点击了搜索按钮")
}

#pragma mark - Scroll View Delegate Methods
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    _currentIndex = scrollView.contentOffset.x / SCREENW;
    _navTabBar.currentItemIndex = _currentIndex;
    
    /** 当scrollview滚动的时候加载当前视图 */
    UIViewController *viewController = (UIViewController *)_subViewControllers[_currentIndex];
    viewController.view.frame = CGRectMake(_currentIndex * SCREENW, 0, SCREENW, _mainView.kdj_height);
    [_mainView addSubview:viewController.view];
    [self addChildViewController:viewController];
}

#pragma mark - SNKNavTabBarDelegate
- (void)itemDidSelectedWithIndex:(NSInteger)index withCurrentIndex:(NSInteger)currentIndex {
    if (currentIndex - index >= 2 || currentIndex - index <= -2) {
        [_mainView setContentOffset:CGPointMake(index * SCREENW, 0) animated:NO];
    } else {
        [_mainView setContentOffset:CGPointMake(index * SCREENW, 0) animated:YES];
    }
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO];
}


@end
